<template>
  <a-modal :zIndex='zIndex' :title="title" :visible="visible" @cancel="handleCancel" width="700px"
    :cancelText="$t('关闭')" :confirmLoading="confirmLoading">
    <template slot="footer">
      <a-button @click="handleCancel">{{ $t('取消') }}</a-button>
      <a-button type="primary" @click="handleOk" v-if="!disable">{{ $t('保存') }}</a-button>
    </template>
    <div style="max-height: 300px; overflow-y: auto" ref="modalForm">
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model ref="dataForm" :rules="rules" :model="dataForm" :label-col="labelCol" :wrapper-col="wrapperCol"
            v-enterToNext>
            <a-row>
              <a-col :span="18">
                <a-form-model-item prop="code" style="margin-bottom: 10px" :label="$t('商品编码')">
                  <a-input v-model="dataForm.code" :disabled="true"></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="18">
                <a-form-model-item prop="name" style="margin-bottom: 10px" :label="$t('商品名称')">
                  <a-input v-model="dataForm.name" :disabled="true"></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="18">
                <a-form-model-item prop="cost" style="margin-bottom: 10px" :label="$t('参考成本')">
                  <a-input v-model="dataForm.cost"></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row style="margin-top: 10px">
              <a-col :span="20" :offset="2" style="margin-top: 10px;margin-bottom: 10px">
                <a-checkbox :checked="dataForm.allowOut == 'Y'" @change="(e) => changeSelectState(e, 'allowOut', 2)">
                  {{ $t('允许0库存出库') }}</a-checkbox>
                <span style="margin-left: 10px; color: orange">
                  {{ $t('（启用后账面库存可以出库为负值，入库后进行冲正）') }}</span>
              </a-col>
            </a-row>
            <a-row style="margin-top: 10px">
              <a-col :span="20" :offset="2" style="margin-top: 10px">
                <a-checkbox :checked="dataForm.allowStock == 1" @change="(e) => changeSelectState(e, 'allowStock', 1)">
                  {{ $t('使用库存管理') }}
                </a-checkbox>
                <span style="margin-left: 10px; color: orange">
                  {{ $t('（启用后吧台开卡、补卡等业务才会进行会员卡出库）') }}</span>
              </a-col>
            </a-row>
          </a-form-model>
        </a-col>
      </a-row>
    </div>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
export default {
  name: 'GoodsInfoCardModal',
  components: {
  },
  data() {
    return {
      zIndex: 1001,
      model: {},
      title: '',
      confirmLoading: false,
      dataForm: {
        code: '',
        name: '',
        full: '0',
        validityType: '0',
        allowGoodsType: '0',
        state: 'Y',
        startDate: '',
        endDate: '',
        startDays: '',
        validDays: '',
      },
      treeData: [],
      deviceShow: false,
      visible: false,
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 15 },
      },
      rules: {
        cost: [
          { required: true, message: this.$t('请输入卖品参考成本') },
          { pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') },
        ],
      },
      url: {
        add: '/goods/bisGoodsInfor/add',
        edit: '/goods/bisGoodsInfor/edit',
      },
      disable: false,
      stateDisable: '',
      taxSetting: localStorage.getItem('taxSetting'),
    }
  },
  mounted() { },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(value) {
      this.islimitValid = true
      this.visible = true
      this.stateDisable = value
      this.dataForm = {
        code: '',
        name: '',
        full: '0',
        price: '0',
        validityType: '0',
        allowGoodsType: '0',
        allowMemberDiscount: 'Y',
        allowTax: 1,
        state: 'Y',
        notAllowGoods: '',
        form: 13,
      }
      this.edit(this.dataForm)
    },
    edit(record, state) {
      this.stateDisable = record.form
      this.disable = state
      this.taxSetting = localStorage.getItem('taxSetting')
      if (record.id) {
        this.edit_disable = true
      }
      this.dataForm = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      let querydata = {}
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          querydata = that.dataForm
          let httpurl = ''
          let method = ''
          if (!that.dataForm.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          that.confirmLoading = true
          httpAction(httpurl, querydata, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    changeSelectState(e, key, type) {
      if (type == 1) {
        this.$set(this.dataForm, key, e.target.checked ? 1 : 0)
      }
      if (type == 2) {
        this.$set(this.dataForm, key, e.target.checked ? 'Y' : 'N')
      }
    },
  },
}
</script>
<style scoped>
.bold-font {
  font-weight: bold;
}
</style>